package cn.edu.aust.myexpri5app;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.apkfuns.logutils.LogUtils;

import org.apache.commons.lang3.StringUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.edu.aust.myexpri5app.UI.RegisterActivity;
import cn.edu.aust.myexpri5app.UI.ShowInfoActivity;
import cn.edu.aust.myexpri5app.model.UserInfo;
import cn.edu.aust.myexpri5app.utils.FileTools;
import cn.edu.aust.myexpri5app.utils.JsonTools;

public class MainActivity extends AppCompatActivity {
    public static final String INNER_USER_INFO_FILE="userinfo.txt";

    private static final int LOGIN_SUCC=0;
    private static final int NO_USER_EXIST=1;
    private static final int PASSWORD_ERROR=2;
    private static final int NO_USER_FILE=3;
    private static final int UNKNOWN_ERROR=-1;

    @BindView(R.id.et_username)
    EditText etUsername;

    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_login)
    public void onClick(View view){
        String username=etUsername.getText().toString().trim();
        String pwd=etPassword.getText().toString().trim();
        LogUtils.d("username=%s, pwd=%s",username,pwd);
        if (pwd.equals("")) pwd=null;

        int res=login(username,pwd);
        Intent intent=null;
        switch(res){
            case LOGIN_SUCC:
                Toast.makeText(MainActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
                intent=new Intent(MainActivity.this, ShowInfoActivity.class);
                startActivity(intent);
                break;
            case NO_USER_FILE:
            case NO_USER_EXIST://用户不存在
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Warnning")
                        .setMessage("需要转入注册页面吗？")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent1=new Intent(MainActivity.this, RegisterActivity.class);
                                intent1.putExtra("username",username);
                                startActivity(intent1);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
                        builder.create().show();
                break;
            case PASSWORD_ERROR://密码错误
                Toast.makeText(MainActivity.this,"密码错误！",Toast.LENGTH_LONG).show();
                break;
            case UNKNOWN_ERROR:
                Toast.makeText(MainActivity.this,"未知错误！",Toast.LENGTH_LONG).show();
                break;

        }

    }

    private int login(String username,String pwd){
        int state=UNKNOWN_ERROR;

        //读取文件内容
        List<UserInfo> userList= null;
        try {
            String origData= FileTools.readDataFromInner(getApplicationContext());
            LogUtils.d("JSON: %s",origData);
            userList = JsonTools.jsonToObjs(origData);
        } catch (IOException e) {
            e.printStackTrace();
            return NO_USER_FILE;
        }
        if (userList!=null && !userList.isEmpty()){
            for (UserInfo u:userList){
                LogUtils.d("JSON: username=%s, password=%s",u.getUsername(),u.getPassword());
                if (StringUtils.equals(u.getUsername(),username)){
                    if (StringUtils.equals(u.getPassword(),pwd)){
                        state=LOGIN_SUCC;
                        break;
                    }else{
                        state=PASSWORD_ERROR;
                        break;
                    }
                }else{
                    Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_LONG).show();
                    state=NO_USER_EXIST;
                }
            }
        }
        return state;
    }
}